home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 104_01 / c4.c < prev    next >
Text File  |  1980-01-01  |  6KB  |  337 lines

  1. /*      >>>>>> start of cc4 <<<<<<<     */
  2. #ifndef TRUE    /* check to see if need include file */
  3. #include <C.DEF>
  4. #endif
  5. keepch(c)
  6. char c;
  7. {
  8.     mline[mptr]=c;
  9.     if (mptr<mpmax) mptr++;
  10.     return c;
  11.     }
  12. preprocess()
  13. {
  14.     int k;
  15.     char c,sname[namesize];
  16.     if (!cmode) return;
  17.     mptr=lptr=0;
  18.     while(!cif && !eof) {
  19.         if (match("#endif")) cif=TRUE;
  20.         else inline();
  21.         }
  22.     if (ch() == '#') match("#endif");
  23.     while(ch()) {
  24.         if (isspace(ch())) {
  25.             keepch(' ');
  26.             while(isspace(ch())) gch();
  27.             }
  28.         else if (ch() == '\\') {
  29.             keepch(ch());
  30.             gch();
  31.             if (!ch()) {
  32.                 error("missing escape character");
  33.                 break;
  34.                 }
  35.             keepch(ch());
  36.             gch();
  37.             }
  38.         else if(ch()=='"') {
  39.             keepch(ch());
  40.             gch();
  41.             while(ch()!='"') {
  42.                 if(!ch()) {
  43.                   error("missing quote");
  44.                   break;
  45.                   }
  46.                 if (ch() == '\\') keepch(gch());
  47.                 keepch(gch());
  48.                 }
  49.             gch();
  50.             keepch('"');
  51.             }
  52.         else if(ch()=='\'') {
  53.             keepch('\'');
  54.             gch();
  55.             while(ch()!='\'') {
  56.                 if(!ch()) {
  57.                   error("missing apostrophe");
  58.                   break;
  59.                   }
  60.                 if (ch()== '\\') keepch(gch());
  61.                 keepch(gch());
  62.                 }
  63.             gch();
  64.             keepch('\'');
  65.             }
  66.         else if((ch()=='/') && (nch()=='*')) {
  67.             inchar();
  68.             inchar();
  69.             while(!((ch()=='*') && (nch()=='/'))) {
  70.                 if(ch()==0)inline();
  71.                 else inchar();
  72.                 if(eof)break;
  73.                 }
  74.             inchar();
  75.             inchar();
  76.             }
  77.         else if(an(ch())) {
  78.             k=0;
  79.             while(an(ch())) {
  80.                 if(k<namemax)sname[k++]=ch();
  81.                 gch();
  82.                 }
  83.             sname[k]=0;
  84.             if(k=findmac(sname))
  85.                 while(c=macq[k++])
  86.                     keepch(c);
  87.             else {
  88.                 k=0;
  89.                 while(c=sname[k++])
  90.                     keepch(c);
  91.                 }
  92.             }
  93.         else keepch(gch());
  94.         }
  95.     keepch(0);
  96.     if(mptr>=mpmax) error("line too long");
  97.     lptr=mptr=0;
  98.     while(line[lptr++]=mline[mptr++]);
  99.     lptr=0;
  100.     }
  101. addmac()
  102. {
  103.     char sname[namesize];
  104.     int k;
  105.     if (symname(sname) == 0) {
  106.         illname();
  107.         kill();
  108.         return;
  109.         }
  110.     k=0;
  111.     while(putmac(sname[k++]));
  112.     while(isspace(ch())) gch();
  113.     while(putmac(gch()));
  114.     if (macptr >= macmax) error("marco table full");
  115.     }
  116. putmac(c)
  117. char c;
  118. {
  119.     macq[macptr]=c;
  120.     if(macptr < macmax) macptr++;
  121.     return c;
  122.     }
  123. findmac(sname)
  124. char *sname;
  125. {
  126.     int k;
  127.     k=0;
  128.     while(k<macptr) {
  129.         if(astreq(sname,macq+k,namemax)) {
  130.             while(macq[k++]);
  131.             return k;
  132.             }
  133.         while(macq[k++]);
  134.         while(macq[k++]);
  135.         }
  136.     return 0;
  137.     }
  138. outbyte(c)
  139. char c;
  140. {
  141.     if (!c) return 0;    /* if null return */
  142.     if (output) {
  143.         if (putc(c,fout) == ERROR) {
  144.             closeout();
  145.             error("Output file error");
  146.             }
  147.         }
  148.     else putch(c);
  149.     return c;
  150.     }
  151. outstr(ptr)
  152. char *ptr;
  153. {
  154.     while(outbyte(*ptr++));
  155.     }
  156. nl()
  157. {
  158.     outbyte('\r');
  159.     outbyte('\n');
  160.     }
  161. tab()
  162. {
  163.     outbyte('\t');
  164.     }
  165. col()
  166. {
  167.     outbyte(':');
  168.     }
  169. error(ptr)
  170. char *ptr;
  171. {
  172.     char buff[80];
  173.     int  j,k;
  174.     strcpy(buff,ptr);
  175.     strcat(buff," \"");
  176.     j=lptr-10;
  177.     if (j<0) j=0;
  178.     k=strlen(buff);
  179.     while(j-15<lptr && line[j]) buff[k++]=line[j++];
  180.     buff[k]=0;
  181.     strcat(buff,"\"");
  182.     error_print(buff);
  183.     return;
  184.     }
  185. error_print(ptr)
  186. char ptr[];
  187. {
  188.     char buff[80];
  189.     int k,j;
  190.     comment();outstr(line);nl();comment();
  191.     k=0;
  192.     while(k<lptr) {
  193.         if(line[k]==9) tab();
  194.             else outbyte(' ');
  195.         ++k;
  196.         }
  197.     outbyte('^');
  198.     nl();comment();outstr("******  ");
  199.     outstr(ptr);
  200.     outstr("  ******");
  201.     nl();
  202.     if (input2) {
  203.         strcpy(buff,"Error in include file at line ");
  204.         sdec(buff+strlen(buff),line2);
  205.         }
  206.     else {
  207.         strcpy(buff,"Error at line ");
  208.         sdec(buff+strlen(buff),line1);
  209.         }
  210.     strcat(buff," ");
  211.     strcat(buff,ptr);
  212.     strcat(buff,"\n");
  213.     pl(buff);
  214.     ++errcnt;
  215.      }
  216. ol(ptr)    
  217. char ptr[];
  218. {
  219.     ot(ptr);
  220.     nl();
  221. }
  222. ot(ptr)
  223. char ptr[];
  224. {
  225.     tab();
  226.     outstr(ptr);
  227.     }
  228. streq(str1,str2)
  229. char str1[],str2[];
  230. {
  231.     int k;
  232.     k=0;
  233.     while(str2[k]) {
  234.         if (str1[k]!=str2[k]) return 0;
  235.         k++;
  236.         }
  237.     return k;
  238.     }
  239. astreq(str1,str2,len)
  240. char str1[],str2[];int len;
  241. {
  242.     int k;
  243.     k=0;
  244.     while (k<len) {
  245.         if ((str1[k])!=(str2[k]))break;
  246.         if(!str1[k])break;
  247.         if(!str2[k])break;
  248.         k++;
  249.         }
  250.     if (an(str1[k]))return 0;
  251.     if (an(str2[k]))return 0;
  252.     return k;
  253.     }
  254. match(lit)
  255. char *lit;
  256. {
  257.     int k;
  258.     blanks();
  259.     if (k=streq(line+lptr,lit))
  260.         {lptr=lptr+k;
  261.         return 1;
  262.         }
  263.     return 0;
  264.     }
  265. amatch(lit,len)
  266. char *lit;int len;
  267.  {
  268.     int k;
  269.     blanks();
  270.     if (k=astreq(line+lptr,lit,len)) {
  271.         lptr=lptr+k;
  272.         while(an(ch())) inbyte();
  273.         return 1;
  274.         }
  275.     return 0;
  276.     }
  277. blanks()
  278. {
  279.     while(1) {
  280.         while(ch()==0) {
  281.             inline();
  282.             preprocess();
  283.             if (eof) break;
  284.             }
  285.         if(isspace(ch())) gch();
  286.         else return;
  287.         }
  288.     }
  289. outdec(number)
  290. int number;
  291. {
  292.     char buff[10];
  293.     outstr(sdec(buff,number));
  294.     }
  295. sdec(string,number)
  296. char string[];
  297. int number;
  298. {
  299.     int k,zs,j;
  300.     char c;
  301.     j=zs=0;
  302.     k=10000;
  303.     if (number<0) {
  304.         number=(-number);
  305.         string[j++]='-';
  306.         }
  307.     while (k>=1) {
  308.         c=number/k + '0';
  309.         if (c != '0' || k == 1 || zs) {
  310.             zs=1;
  311.             string[j++]=c; 
  312.             }
  313.         number=number%k;
  314.         k=k/10;
  315.         }
  316.     string[j]=0;
  317.     return string;
  318.     }
  319. /*                        */
  320. /*    to output number as a hex value        */
  321. /*                        */
  322. /*    written june 28, 1981 by Mike Bernson    */
  323. /*                        */
  324. outhex(value)
  325. int value;
  326. {
  327.     int  postion;
  328.     char *hex;
  329.  
  330.     hex="0123456789abcdef";
  331.     for(postion=16; postion >= 0; postion -=4) 
  332.         outbyte(hex[(value>>postion) & 0x0f]);
  333.     outbyte('h');
  334.     } 0x0f]);
  335.     outbyte('h');
  336.     }/* number of case statement in switch */
  337. int label;    /* label for switch t